//
//  VipCardClassSelectViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/31.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

extern NSString * const YZUpdateMenuTitleNote;
static NSString * const categoryID = @"categoryID";
static NSString * const categoryDetailID = @"categoryDetailID";

#import "VipCardClassSelectViewController.h"
#import "VipInfoTableViewCell.h"
@interface VipCardClassSelectViewController ()<UITableViewDelegate,UITableViewDataSource>

/**
 *  分类tableView
 */
@property (weak, nonatomic) UITableView *categoryTableView;
/**
 *  分类详情tableView
 */
@property (weak, nonatomic) UITableView *categoryDetailTableView;

@property (strong, nonatomic) NSString *selectedCategory;

@property (nonatomic,strong)NSMutableArray *categoryArray;

@property (nonatomic,strong)NSMutableArray *categoryDetailArray;

@property (nonatomic,assign)NSInteger selectCategoryRow;

@end

@implementation VipCardClassSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UITableView *categoryTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    categoryTableView.dataSource = self;
    categoryTableView.delegate = self;
    categoryTableView.showsVerticalScrollIndicator = NO;
    [categoryTableView registerClass:[VipInfoTableViewCell class] forCellReuseIdentifier:categoryID];
    
    categoryTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    categoryTableView.backgroundColor = BackGroundColor;
    [self.view addSubview:categoryTableView];
    self.categoryTableView = categoryTableView;
    
    UITableView *categoryDetailTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    categoryDetailTableView.dataSource = self;
    categoryDetailTableView.delegate = self;
    categoryDetailTableView.showsVerticalScrollIndicator = NO;
    [categoryDetailTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:categoryDetailID];
    categoryDetailTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    categoryDetailTableView.backgroundColor = BackGroundColor;
    [self.view addSubview:categoryDetailTableView];
    self.categoryDetailTableView = categoryDetailTableView;
    
    [categoryTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view).multipliedBy(0.5);
    }];
    
    [categoryDetailTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self.view);
        make.left.mas_equalTo(categoryTableView.mas_right);
        make.width.mas_equalTo(self.view).multipliedBy(0.5);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selfViewTap)];
    
    
    self.selectCategoryRow = 0;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self.categoryTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
//    [self tableView:self.categoryTableView didSelectRowAtIndexPath:indexPath];
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.categoryTableView) {
        // 左边的类别表格 👈
        return self.categoryArray.count;
        
    } else {
        // 右边的类别详情表格 👉
        
        NSArray *categoryDetailArray = self.categoryDetailArray[self.selectCategoryRow];
        
        if (categoryDetailArray) {
            
            return categoryDetailArray.count;
        }
        
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.categoryTableView) {
        // 左边的类别表格 👈
        UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:categoryID];
        cell.textLabel.font = MidFont;
        cell.textLabel.text = [NSString stringWithFormat:@"%@",[self.categoryArray objectAtIndex:indexPath.row]];
        
        return cell;
    }
    
    // 右边的类别详情表格 👉
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:categoryDetailID];
    cell.textLabel.font = MidFont;
    NSArray *categoryDetailArray = self.categoryDetailArray[self.selectCategoryRow];
    
    VipCardModel *vipCardModel = [categoryDetailArray objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@",vipCardModel.MemberCardName];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.categoryTableView) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        // 左边的类别表格 👈
        _selectedCategory = cell.textLabel.text;
        
        self.selectCategoryRow = indexPath.row;
        
        // 刷新右边数据
        [self.categoryDetailTableView reloadData];
        
        // 更新菜单标题
        [[NSNotificationCenter defaultCenter] postNotificationName:YZUpdateMenuTitleNote object:self userInfo:@{@"title":cell.textLabel.text,@"col":@(self.colCount)}];
        
        return;
    }
    
    // 右边的类别详情表格 👉
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    
}

#pragma mark Tap
-(void)selfViewTap{
    
    // 更新菜单标题
    [[NSNotificationCenter defaultCenter] postNotificationName:YZUpdateMenuTitleNote object:self userInfo:@{@"title":@"",@"col":@(self.colCount)}];
}


-(NSMutableArray *)categoryArray{
    
    if (!_categoryArray) {
        
        _categoryArray = [[NSMutableArray alloc] init];
        
        //[_categoryArray addObject:@"全部类型"];
        [_categoryArray addObject:@"储值类型"];
        [_categoryArray addObject:@"次卡类型"];
        [_categoryArray addObject:@"期限类型"];
    }
    
    return _categoryArray;
}


-(void)setDataSource:(NSMutableArray *)dataSource{
    
    NSMutableArray *valueArray = [[NSMutableArray alloc] init];
    
    NSMutableArray *timeArray = [[NSMutableArray alloc] init];
    
    NSMutableArray *termArray = [[NSMutableArray alloc] init];
    
    for (NSDictionary *dict in dataSource) {
                        
        VipCardModel *vipCardModel = [VipCardModel mj_objectWithKeyValues:dict];
        
        if ([vipCardModel.MemberCardType intValue] == Time) {
            
            [timeArray addObject:vipCardModel];
        }
        else if ([vipCardModel.MemberCardType intValue] == Value){
            
            [valueArray addObject:vipCardModel];
        }
        else{
            
            [termArray addObject:vipCardModel];
        }
    }
    
    [self.categoryDetailArray removeAllObjects];
    
    [self.categoryDetailArray addObject:valueArray];
    
    [self.categoryDetailArray addObject:timeArray];
    
    [self.categoryDetailArray addObject:termArray];
    
    [_categoryTableView reloadData];
}

-(NSMutableArray *)categoryDetailArray{
    
    if (!_categoryDetailArray) {
        
        _categoryDetailArray = [[NSMutableArray alloc] init];
        
    }
    
    return _categoryDetailArray;
}
@end
